//
//  DetailsTVController.m
//  Travel assistant
//
//  Created by lanou3g on 16/2/28.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "DetailsTVController.h"
#import "ConsumerTopCell.h"
#import "ConsumerTVCell.h"
#import "RemindTableViewCell.h"
#import "AddConsumeVController.h"
#import "AddNotesVController.h"
@interface DetailsTVController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *consumerArray;

@end

@implementation DetailsTVController
static NSString *const consumerTopCell = @"ConsumerTopCell";
static NSString *const consumerTVCell = @"ConsumerTVCell";
static NSString *const remindTableViewCell = @"RemindTableViewCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
    [self setDelegate];
    [self setRegisterCell];
    [self setDataFromCleanCloud];
    

    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewWillAppear:(BOOL)animated{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}
- (void)setDataFromCleanCloud{
    AVQuery *consumerQuery = [ConsumptioModel query];
    [consumerQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.consumerArray = [NSMutableArray array];
        if (objects.count) {
            NSArray<AVObject *> *queryArray = objects;
            for (ConsumptioModel *consumptioModel in queryArray) {
                ConModel *model = [[ConModel alloc] init];
                model.consumType = [consumptioModel objectForKey:@"consumType"];
                model.consumAmount = [consumptioModel objectForKey:@"consumAmount"];
                model.consumCurrency = [consumptioModel objectForKey:@"consumCurrency"];
                model.consumTime = [consumptioModel objectForKey:@"consumTime"];
                model.consumNote = [consumptioModel objectForKey:@"consumNote"];
                [self.consumerArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
}];
    
    
    
    
    
}
- (void)setViews{
    JourneyTypeModel *model = self.journeyArrList[self.receivedIndex];
    self.title = model.type;
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_32px_1199462_easyicon.net"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
}
- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender{
    

    if (self.receivedIndex == 0) {
        NSLog(@"添加消费........");
        AddConsumeVController *addConsumeVC = [[AddConsumeVController alloc] init];
        [self.navigationController pushViewController:addConsumeVC animated:YES];
    } else {
        NSLog(@"添加笔记。。。。。。。。");
        AddNotesVController *addNotesVC = [[AddNotesVController alloc] init];
        [self.navigationController pushViewController:addNotesVC animated:YES];
        
    }
}
- (void)setDelegate{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
- (void)setRegisterCell{
    [self.tableView registerNib:[UINib nibWithNibName:@"ConsumerTopCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:consumerTopCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"ConsumerTVCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:consumerTVCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"RemindTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:remindTableViewCell];
}
/**懒加载*/
- (NSArray *)journeyArrList{
    if (!_journeyArrList) {
        _journeyArrList = [NSArray arrayWithArray:[ShareTypeDataHeleper shareTypeDataHeleper].typeArray];
    }
    return _journeyArrList;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (self.receivedIndex) {
        case 0:
            if (section == 0) {
                return 1;
            }else {
                return self.consumerArray.count;
            }
            break;
    }
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /**当天消费栏Cell*/
    ConsumerTopCell *topTell;
    /**当天消费Cell*/
    ConsumerTVCell *otherTVCell;
//    /**笔记提醒的Cell*/
    RemindTableViewCell *remindCell;
    /**闲来无事返回的Cell*/
    UITableViewCell *cell;
    switch (self.receivedIndex) {
        case 0:
            if (indexPath.section == 0) {
                topTell = [tableView dequeueReusableCellWithIdentifier:consumerTopCell forIndexPath:indexPath];
                NSInteger allAmount = 0;
                for (ConModel *model in self.consumerArray) {
                    NSInteger amount = [model.consumAmount integerValue];
                    allAmount += amount;
                }
                topTell.allamountLable.text = [NSString stringWithFormat:@"%ld",allAmount];
                ConModel *model = self.consumerArray.lastObject;
                topTell.dateLable.text = model.consumTime;
                return topTell;
            } else {
                NSLog(@"dsgfmdlksgsdkfgnldksgfkldsglkdsg");
                otherTVCell = [tableView dequeueReusableCellWithIdentifier:consumerTVCell forIndexPath:indexPath];
                ConModel *model = self.consumerArray[indexPath.row];
                otherTVCell.conModel = model;
                return otherTVCell;
                break;
            }
        case 1:
            remindCell = [tableView dequeueReusableCellWithIdentifier:remindTableViewCell forIndexPath:indexPath];
            return remindCell;
            break;
    }
    return cell;
}
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (self.receivedIndex) {
        case 0:
            if (indexPath.section == 0) {
                return 40;
            }
            return 85;
            break;
        case 1:
            return 85;
            break;
    }
    return 100;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
